<template>
  <view class="recruit-favorite-container">
    <view class="recruit-favorite-header">
      <text class="recruit-favorite-title">我的收藏</text>
      <text class="recruit-favorite-count">
        共{{ favorites.length }}个收藏
      </text>
    </view>
    <view v-if="favorites.length > 0" class="recruit-favorite__list">
      <recruit-favorite-item
        v-for="item in favorites" 
        :key="item.id"
        :item="item"
        :default-logo="defaultLogo"
        @detail="goToDetail"
        @cancel="handleCancelFavorite"
        @apply="goToApply"
      />
    </view>
    
    <!-- 空状态 -->
    <view v-else-if="!loading" class="recruit-favorite__empty">
      <image class="recruit-favorite__empty-image" src="https://placekitten.com/300/200" mode="aspectFit"></image>
      <text class="recruit-favorite__empty-text">暂无收藏记录</text>
      <button class="recruit-favorite__empty-btn" @click="goToRecruitList">浏览招聘</button>
    </view>
    
    <!-- 加载中 -->
    <view v-else class="recruit-favorite__loading">
      <text>加载中...</text>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useStore } from 'vuex';
import RecruitFavoriteItem from '@/components/recruit/RecruitFavoriteItem.vue';

// 初始化store
const store = useStore();

// 计算属性：收藏列表
const favorites = computed(() => store.getters['recruit/getUserFavorites']);

// 默认logo
const defaultLogo = ref('https://placekitten.com/200/200');

// 计算属性：加载状态
const loading = computed(() => store.getters['recruit/loading']);

// 页面加载时获取数据
onMounted(() => {
  store.dispatch('recruit/fetchUserFavorites');
});

// 获取岗位类型名称
const getTypeName = (type) => {
  const typeMap = {
    0: '全职',
    1: '兼职',
    2: '实习',
    3: '志愿者'
  };
  return typeMap[type] || '未知';
};

// 取消收藏
const handleCancelFavorite = async (id) => {
  try {
    const result = await store.dispatch('recruit/toggleFavorite', {
      id,
      isFavorite: true
    });
    
    if (result.success) {
      uni.showToast({
        title: result.message || '已取消收藏',
        icon: 'success'
      });
    } else {
      uni.showToast({
        title: result.message || '操作失败',
        icon: 'none'
      });
    }
  } catch (error) {
    console.error('取消收藏失败', error);
    uni.showToast({
      title: '操作失败',
      icon: 'none'
    });
  }
};

// 跳转到岗位详情
const goToDetail = (id) => {
  uni.navigateTo({
    url: `/pages/recruit/detail?id=${id}`
  });
};

// 跳转到申请页面
const goToApply = (id) => {
  uni.navigateTo({
    url: `/pages/recruit/apply?id=${id}`
  });
};

// 跳转到招聘列表
const goToRecruitList = () => {
  uni.switchTab({
    url: '/pages/recruit/index'
  });
};
</script>

<style lang="scss" scoped>
.recruit-favorite-container {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding-bottom: 20px;
}

.recruit-favorite-header {
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  margin-bottom: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.recruit-favorite-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.recruit-favorite-count {
  font-size: 14px;
  color: #999;
}

.recruit-favorite__list {
  padding: 12px 16px;
}

.recruit-favorite__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  padding: 0 30px;
  
  &-image {
    width: 200px;
    height: 150px;
    margin-bottom: 20px;
  }
  
  &-text {
    font-size: 16px;
    color: #999;
    margin-bottom: 20px;
  }
  
  &-btn {
    height: 40px;
    border-radius: 20px;
    background-color: #1890ff;
    color: #ffffff;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 30px;
  }
}

.recruit-favorite__loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 300px;
  color: #999;
  font-size: 14px;
}
</style>
